#!/usr/bin/python
# _*_ coding:UTF-8 _*_
import json
import requests
import urllib
import sys
import datetime
import logging
import os

if len(sys.argv) == 1:
    print '\033[1;31;40m'
    print "The Usage of this script is :\r"
    print "        "+sys.argv[0] + "  1875693xxxx b0041028  'The load of the server over 1000'"
    print '\033[0m'
    os._exit(0)
#define the time of sending message
date_now = datetime.datetime.now().strftime('%m-%d %H:%M ')
#define the level fo log
logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(message)s',
                datefmt='%a, %Y-%m-%d %H:%M:%S',
                filename='sendmsg.log',
                filemode='a')
#detail of the message
msg_detail={
	"monitortime":date_now,
	"monitortype":"主机",
	"monitorname":sys.argv[2],
	"state":"详情",
	"statedes":sys.argv[3]
}
#encoding msg_detail to json
msg_detail = json.dumps(msg_detail,ensure_ascii=False)
#params of the url
msg_params={
"apikey":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"password":"xxxxxxxxx",
"mobile":sys.argv[1],
"templateid":10080,
"templateparams":msg_detail
}
try :
	Response = requests.post('http://sms.openspeech.cn/api/send?',params=msg_params)
	if  Response.status_code == 200:
		logging.info(sys.argv)
except requests.exceptions.ConnectionError,e:
		logging.exception(e)